<template>
  <view class="container" :style="appThemeStyle">
    <commodity ref="mescrollItem" :list="list" @scan="handleScan" @toGoods="toGoods"/>
  </view>
</template>
<script>
import MescrollCompMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mixins/mescroll-comp'
import Search from '@/components/search'
import * as GoodsApi from '@/api/goods'
import Commodity from '../components/commodity'
import * as StoreApi from "@/api/store"
import store from '@/store'
import ScanCodeTypeEnum from "@/common/enum/scanCodeType"

export default {
  name: "index",
  components: {
    Search,
    Commodity
  },
  mixins: [MescrollCompMixin],
  data() {
    return {
      list: []
    }
  },
  computed: {
    store() {
      return store.getters.store
    },
  },
  onLoad() {
    this.initCategory()
  },
  methods: {
    /**
     * 初始化分类列表数据
     */
    initCategory() {
      GoodsApi.categoryTree().then(({ data }) => {
        this.list = data
      }).catch(() => {})
    },
    handleScan() {
      uni.scanCode({
        success: res => {
          StoreApi.scanCode({
            type: ScanCodeTypeEnum.GOODS_BARCODE.value,
            scanResult: res.result,
            shopId: this.store.id
          }).then(({ data }) => {
            this.$navTo('goodspages/goods/index', { barcode: data.barcode })
          }).catch(({msg}) => {
            this.$toast(msg ||'未识别到该商品')
          })
        },
        fail: (err) => {
          this.$toast('未识别到该商品')
        }
      })

    },
    toGoods() {
      this.$navTo('goodspages/goods/index')
    }
  }
}
</script>

<style>
page {
    background: #fff;
}
</style>

<style scoped lang="scss">
.container {
}
</style>
